var compareVersions = function (v1, v2) {
	var n1 = validateAndParse(v1);
	var n2 = validateAndParse(v2);

	var p1 = n1.pop();
	var p2 = n2.pop();
	// validate numbers
	var r = compareSegments(n1, n2);
	if (r !== 0)
		return r;
	if (p1 && p2) {
		return compareSegments(p1.split('.'), p2.split('.'));
	}
	else if (p1 || p2) {
		return p1 ? -1 : 1;
	}
	return 0;
};

var semver = /^[v^~<>=]*?(\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+)(?:\.([x*]|\d+))?(?:-([\da-z\-]+(?:\.[\da-z\-]+)*))?(?:\+[\da-z\-]+(?:\.[\da-z\-]+)*)?)?)?$/i;
var validateAndParse = function (version) {
	if (typeof version !== 'string') {
		throw new TypeError('Invalid argument expected string');
	}
	var match = version.match(semver);
	if (!match) {
		throw new Error("Invalid argument not valid semver ('".concat(version, "' received)"));
	}
	match.shift();
	return match;
};
var tryParse = function (v) {
	var n = parseInt(v, 10);
	return isNaN(n) ? v : n;
};
var forceType = function (a, b) {
	return typeof a !== typeof b ? [String(a), String(b)] : [a, b];
};
var compareStrings = function (a, b) {
	var _a = forceType(tryParse(a), tryParse(b)), ap = _a[0], bp = _a[1];
	if (ap > bp)
		return 1;
	if (ap < bp)
		return -1;
	return 0;
};
var compareSegments = function (a, b) {
	for (var i = 0; i < Math.max(a.length, b.length); i++) {
		var r = compareStrings(a[i] || '0', b[i] || '0');
		if (r !== 0)
			return r;
	}
	return 0;
};
// console.log(compareVersions("2.0.1", "2.0.1"));
exports.compareVersions = compareVersions;
